package org.leon.mapper;

import org.apache.ibatis.annotations.*;
import org.apache.ibatis.cache.impl.PerpetualCache;
import org.leon.pojo.Role;
import org.leon.pojo.User;

import java.util.List;

/**
 * Created by leon.L on 2019/12/25.
 * Desc :
 */
@CacheNamespace(implementation = PerpetualCache.class)
public interface UserMapper {

    List<User> findAll();

    void insertOne(User user);

    List<User> findByIds(List<Integer> ids);

    List<User> findAllWithOrders();

    List<User> findUsersWithRole();

    @Insert("insert into t_user(name, password) values(#{name}, #{password})")
    void insertUser(User user);

    @Update("update t_user set name=#{name} where id=#{id}")
    void updateUser(User user);

    @Select("select id, name from t_user")
    List<User> findUsers();

    @Select("select id, name from t_user where id=#{id}")
    User findById(Integer id);

    @Results({
            @Result(property = "id", column = "id"),
            @Result(property = "name", column = "name"),
            @Result(property = "password", column = "password"),
            @Result(property = "orderList", column = "id", javaType = List.class,
                    many = @Many(select = "org.leon.mapper.OrderMapper.findOrderByUserId"))
    })
    @Select("select id, name, password from t_user")
    List<User> findUserOrderByAnon();

    @Results({
            @Result(property = "id", column = "id"),
            @Result(property = "name", column = "name"),
            @Result(property = "password", column = "password"),
            @Result(property = "roleList", column = "id", javaType = List.class,
                    many = @Many(select = "org.leon.mapper.RoleMapper.findRoleByUserIdByAnon"))
    })
    @Select("select id, name, password from t_user")
    List<User> findUserRoleByAnon();
}
